#!/bin/sh

test_description="test proc-receive"

. ./lib/sharness.sh

REPO_ID=""

#创建repo
test_expect_success "[test-1] init repo and branch" '
	REPO_ID=$(find_repo | jq .id)
	if [ -n $REPO_ID ]; then
		delete_repo > delete_repo.resp 2>&1
		test_expect_code 0
	fi
	create_repo > create_repo.resp 2>&1
	REPO_ID=$(cat create_repo.resp) &&
	add_member $WRITE_LEVEL $another_user_external &&
	init_repo > init_repo.resp &&
	test_expect_code 0
'

#创建 master 分支
test_expect_success "[test-2] normal push success" '
	echo "This is the first commit" > "$REPO_DIR"/README &&
	git -C "$REPO_DIR" add README &&
	git -C "$REPO_DIR" commit -m "first commit" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	git -C "$REPO_DIR" push origin master > test-2 2>&1 &&
	test_expect_code 0
'

#smart模式创建新mr
test_expect_success "[test-3][smart][no existing mr] smart mode create new mr success" '
	add_member $READ_LEVEL $another_user_external &&
	echo "[test-2][smart][no existing mr] smart mode create new mr success" >> "$REPO_DIR"/test.file &&
	git -C "$REPO_DIR" add test.file &&
	git -C "$REPO_DIR" commit -m "smart mode create new mr success" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	git -C "$REPO_DIR" push origin master -o review=smart > test-3 2>&1 &&
	grep "has been created" test-3
'

#smart模式更新mr
test_expect_success "[test-4][smart][single existing mr] smart mode update mr success" '
	echo "[test-3][smart][update single existing mr] smart mode update mr success" >> "$REPO_DIR"/test.file &&
	git -C "$REPO_DIR" add test.file &&
	git -C "$REPO_DIR" commit -m "smart mode update single existing mr" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	git -C "$REPO_DIR" push origin master -o review=smart > test-4 2>&1 &&
	grep "has been updated" test-4
'

#amend提交，同一用户更新成功
test_expect_success "[test-5][smart][single existing mr][diverge] local branch diverged, same user smart mode update mr success" '
	echo "[test-4][smart][update single existing mr][diverge] local branch diverged, same user smart mode update mr success" >> "$REPO_DIR"/test.file &&
	git -C "$REPO_DIR" add test.file &&
	git -C "$REPO_DIR" commit --amend -m "smart mode update single existing mr" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	git -C "$REPO_DIR" push origin master -o review=smart > test-5 2>&1 &&
	grep "has been updated" test-5
'

#new模式创建mr成功
test_expect_success "[test-6][new] creating new mr success" '
	echo "[test-5][new] creating new mr success" >> "$REPO_DIR"/test.file &&
	git -C "$REPO_DIR" add test.file &&
	git -C "$REPO_DIR" commit -m "creating new mr success" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	git -C "$REPO_DIR" push origin master -o review=new > test-6 2>&1 &&
	grep "has been created" test-6
'

#两个mr，smart模式更新失败
test_expect_success "[test-7][smart][multiple existing mr] multiple mr, can not distinguish which one to update" '
	echo "[test-6][smart][multiple existing mr] multiple mr, can not distinguish which one to update" >> "$REPO_DIR"/test.file &&
	git -C "$REPO_DIR" add test.file &&
	git -C "$REPO_DIR" commit -m "multiple mr, can not distinguish which one to update" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	test_must_fail git -C "$REPO_DIR" push origin master -o review=smart > test-7 2>&1 &&
	grep "failed to push some refs" test-7
'

#以下针对保护分支的的测试
#读权限推保护分支创建mr
test_expect_success "[test-8][smart][protected] read permission push to protected branch create mr success" '
	close_merge_request $REPO_ID 1 &&
	close_merge_request $REPO_ID 2 &&
	add_member $READ_LEVEL $another_user_external &&
	protect_branch master &&
	echo "[test-8][smart][protected] read permission push to protected branch" >> "$REPO_DIR"/test.file &&
	git -C "$REPO_DIR" add test.file &&
	git -C "$REPO_DIR" commit -m "read permission push to protected branch" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	git -C "$REPO_DIR" push origin master -o review=smart > test-8 2>&1 &&
	grep "has been created" test-8
'

#读权限推保护分支更新mr成功
test_expect_success "[test-9][smart][protected] read permission author push protected branch update mr success" '
	echo "[test-9][smart][protected] read permission author update mr success" >> "$REPO_DIR"/test.file &&
	git -C "$REPO_DIR" add test.file &&
	git -C "$REPO_DIR" commit -m "read permission author push protected branch update mr success" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	git -C "$REPO_DIR" push origin master -o review=smart > test-8 2>&1 &&
	grep "has been updated" test-8
'

test_expect_success "[test-10] clean up" '
  delete_repo "$REPO_ID" > test-10 2>&1
'

test_done
